/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Auction;
import model.User;
import model.UserType;

/**
 * A GUI used to display the Auctions in a list format.  
 * To get the panel use the getPanel method created.
 * 
 * @author Anthony Rios
 * @author Revised by Travis Drake
 * 
 * @version 05/27/2012
 *
 */
public class AuctionListGUI extends Observable{
		
	private JPanel my_item_panel;
	
	private JList my_list_drop_down;
	
	private User my_user;
	
	private final JButton my_back_button = new JButton("Back");
	
	private final JButton my_view_auction_button = new JButton("View Auction");
	
	private final JButton my_add_auction_button = new JButton("Add Auction");
	
	/**
	 * the constructor for this GUI.  Creates an observable panel.  To get the panel
	 * use the getPanel method.
	 * the_user is used to determine which buttons to display.
	 * 
	 * @required the_auction_list pre_sorted.
	 * @required the_user != null
	 * 
	 */
	public AuctionListGUI(List<Auction> the_auction_list, User the_user) {
		my_user = the_user;
		my_item_panel = new JPanel();
		my_item_panel.setName("List of Current Auctions");
		my_item_panel.setLayout(new BorderLayout());
		my_list_drop_down = new JList(the_auction_list.toArray());
		my_item_panel.add(my_list_drop_down);
		JPanel a_flow_layout = new JPanel(new FlowLayout());
		my_back_button.addActionListener(new ButtonListener());
		my_view_auction_button.addActionListener(new ButtonListener());
		my_add_auction_button.addActionListener(new ButtonListener());
		my_add_auction_button.setName("Add Auction");
		a_flow_layout.add(my_back_button);
		a_flow_layout.add(my_view_auction_button);
		if (my_user.getType() == UserType.AUCTION_CENTRAL || 
				my_user.getType() == UserType.NPO) {
			a_flow_layout.add(my_add_auction_button);
		}
		my_item_panel.add(a_flow_layout, BorderLayout.SOUTH);
		my_item_panel.setVisible(true);
	}

	public JPanel getPanel() {
		return my_item_panel;
	}
	
	/**
	 * displays the error when the button view auction is clicked but an auction was
	 * not selected.
	 */
	public void displayError()
    {
        JOptionPane.showMessageDialog(null, "You did not select an Auction");
    }
	
    /**
     * an Action listener for the undo button.
     * 
     * @author Anthony Rios
     * @version 11-28-11
     */
    protected class ButtonListener implements ActionListener
    {
    	
     /**
      * checks to see if the_event is the view auction button, back button, or add auction button.
      * If its the view auction button 
      */
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
    	  if (the_event.getActionCommand().equals(my_view_auction_button.getActionCommand())) {
        	  if (my_list_drop_down.getSelectedValue() == null) {
        		  displayError();
        	  }
        	  else {
        		  setChanged();
        		  notifyObservers(my_list_drop_down.getSelectedValue());
        	  }
    	  }
    	  else if (the_event.getActionCommand().equals(my_back_button.getActionCommand())) {
    		  setChanged();
    		  notifyObservers(ItemViewGUI.BACK); 
    	  } 
    	  else {
    		  setChanged();
    		  notifyObservers(AuctionViewGUI.ADD_AUCTION);
    	  }
       }
    }
}
